package com.ad.blogger.service.impl;

import java.util.List;
import java.util.regex.Matcher;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ad.blogger.dao.IWeiboManageDAO;
import com.ad.blogger.entity.WeiboAccounts;
import com.ad.blogger.service.IWeiboManageService;
import com.ad.common.utils.PatternUtils;
import com.ad.common.utils.StringUtils;

public class WeiboManageServiceImpl implements IWeiboManageService {
	private final Log logger = LogFactory.getLog(getClass());
	private IWeiboManageDAO weiboManageDAO ;

	public List<WeiboAccounts> getWeiboByAccountName(Integer userId,String accountName) {
		
		return this.weiboManageDAO.findWeiboByAccountName(userId,accountName);
	}

	public IWeiboManageDAO getWeiboManageDAO() {
		return weiboManageDAO;
	}

	public void setWeiboManageDAO(IWeiboManageDAO weiboManageDAO) {
		this.weiboManageDAO = weiboManageDAO;
	}

	public List<WeiboAccounts> getWeiboAccountsByExample(WeiboAccounts instance) {
		return null;
	}

	public List<WeiboAccounts> getWeiboAccountsByUserId(Integer userId) {
	
		return this.weiboManageDAO.findWeiboAccountsByUserId(userId);
	}

	public void save(WeiboAccounts weiboAccounts) {
		this.weiboManageDAO.save(weiboAccounts);
	}

	@Override
	public WeiboAccounts parseWeiboAccountsFromContent(Integer platform,String nickname,String content) {
		WeiboAccounts weibo = new WeiboAccounts();
		weibo.setPlatform(platform);
		if(platform==1){ // 新浪微博
		   Matcher matcher = PatternUtils.SINA_PATTERN.matcher(content);
		   String tmp = null;
		   while(matcher.find()){
			   // 微博昵称
			  tmp = matcher.group(2);
			  System.out.println(StringUtils.decodeUnicode(tmp.trim()));
			  if(tmp!=null &&(tmp= StringUtils.decodeUnicode(tmp.trim())).equals(nickname)){
				  weibo.setNickname(tmp);
			  }else{
				 continue ;
			  }
			  // 微博地址
			  tmp = matcher.group(1);
			  if(tmp!=null){
				  weibo.setUrl(tmp);
			  }
			  // 头像地址
			  tmp = matcher.group(3);
			  if(tmp!=null){
				  weibo.setIconUrl(tmp);
			  }
			  // 性别
			  tmp = matcher.group(5);
			  if(tmp!=null){
				  tmp = StringUtils.decodeUnicode(tmp.trim());
				  if("男".equals(tmp)){
					  weibo.setSex(1);
				  }else{
					  weibo.setSex(0);
				  }
			  }
			  // 地址
			  tmp = matcher.group(6);
			  if(tmp!=null){
				  tmp = StringUtils.decodeUnicode(tmp);
				  String[] addr = tmp.split("，");
				  if(addr.length==2){
					  weibo.setProvince(addr[0]);
					  weibo.setCity(addr[1]);
				  }
			  }
			  // 粉丝数
			  tmp = matcher.group(8);
			  if(tmp!=null){
				  weibo.setFansNum(StringUtils.decodeUnicode(tmp.trim()));
			  }
			  // 微博数
			  tmp = matcher.group(9);
			  if(tmp!=null){
				  weibo.setWeiboNum(StringUtils.decodeUnicode(tmp.trim()));
			  }
			  // 简介
			  tmp = matcher.group(11);
			  if(tmp!=null){
				  weibo.setIntro(StringUtils.decodeUnicode(tmp.trim()));
			  }
			  // 标签
			  tmp = matcher.group(13);
			  if(tmp!=null){
				  tmp = StringUtils.decodeUnicode(tmp.trim());
				  tmp = tmp.replaceAll("<a.*?>", "");
				  tmp = tmp.replaceAll("</a>", ",");
				  weibo.setTags(tmp);
			  }
			  break;
		   }
		}else if(platform==2){//腾讯微博
			Matcher matcher = PatternUtils.QQ_PATTERN.matcher(content);
			String tmp = null ;
			while(matcher.find()){
				// 头像地址
				tmp = matcher.group(1);
				if(tmp!=null){
					weibo.setIconUrl(tmp);
				}
				// 昵称
				tmp = matcher.group(2);
				if(tmp!=null){
					weibo.setNickname(tmp);
				}
				// 广播
				tmp = matcher.group(4);
				if(tmp!=null && tmp.trim().equals("广播")){
					tmp = matcher.group(5);
					if(tmp!=null){
						weibo.setWeiboNum(tmp);
					}
				}
				// 听众
				tmp = matcher.group(7);
				if(tmp!=null && tmp.trim().equals("听众")){
					tmp = matcher.group(8);
					if(tmp!=null){
						weibo.setFansNum(tmp);
					}
				}
				
			}
			// 腾讯QQ企业版
			if(weibo.getNickname() == null){
				matcher = PatternUtils.QQ_PATTERN_COMPANY.matcher(content);
				while(matcher.find()){
					// 头像地址
					tmp = matcher.group(1);
					if(tmp!=null){
						weibo.setIconUrl(tmp);
					}
					// 昵称
					tmp = matcher.group(2);
					if(tmp!=null){
						weibo.setNickname(tmp);
					}
					// 听众
					tmp = matcher.group(3);
					if(tmp!=null && tmp.trim().equals("听众")){
						tmp = matcher.group(4);
						if(tmp!=null){
							weibo.setFansNum(tmp);
						}
					}
					// 广播数
					tmp = matcher.group(7);
					if(tmp!=null && tmp.trim().equals("广播")){
						tmp = matcher.group(8);
						if(tmp!=null){
							weibo.setWeiboNum(tmp);
						}
					}
				}
			}
		}
		if(weibo.getNickname()==null){
			return null ;
		}else{
			addDefaultValue(weibo);
			return weibo ;
		}
		
	}
	private void addDefaultValue(WeiboAccounts weibo){
		//是否接单
		if(weibo!=null){
			weibo.setAccessOrder(1);
			weibo.setIsDirect(1);
			weibo.setAccessHardAd(1);
			weibo.setVerify(0);
		}
	}

}
